// 禁止 rust 启动一个 console
// #![windows_subsystem = "windows"]


// extern crate native_windows_gui as nwg;
// extern crate native_windows_derive as nwd;

// use nwd::NwgUi;
// use nwg::NativeUi;


// #[derive(Default, NwgUi)]
// pub struct BasicApp {
//     #[nwg_control(size: (300, 115), position: (300, 300), title: "Basic example", flags: "WINDOW|VISIBLE")]
//     #[nwg_events( OnWindowClose: [BasicApp::say_goodbye] )]
//     window: nwg::Window,

//     #[nwg_control(text: "Heisenberg", size: (280, 25), position: (10, 10))]
//     name_edit: nwg::TextInput,

//     #[nwg_control(text: "Say my name", size: (280, 60), position: (10, 40))]
//     #[nwg_events( OnButtonClick: [BasicApp::say_hello] )]
//     hello_button: nwg::Button
// }

// impl BasicApp {

//     fn say_hello(&self) {
//         nwg::simple_message("Hello", &format!("Hello {}", self.name_edit.text()));
//     }
    
//     fn say_goodbye(&self) {
//         nwg::simple_message("Goodbye", &format!("Goodbye {}", self.name_edit.text()));
//         nwg::stop_thread_dispatch();
//     }

// }

// fn main() {
//     nwg::init().expect("Failed to init Native Windows GUI");

//     let _app = BasicApp::build_ui(Default::default()).expect("Failed to build UI");

//     nwg::dispatch_thread_events();
// }

use actix_web::{web, App, HttpServer};

const BG_PORT: u16 = 8080;

#[actix_web::get("/")]
async fn index() -> &'static str {
    println!("index");
    "Hello World 1111111111"
}

async fn start_bg() -> std::io::Result<()> {
    let web_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
    let web_dir = std::path::Path::new(&web_dir).join("web").to_str().unwrap().to_string();
    println!("web_dir: {}", &web_dir);
    let server = HttpServer::new(move || {
        App::new()
            .wrap(actix_web::middleware::Logger::default())
            .service(
                web::scope("/api")
                    .service(index)
            )
            .service(
                actix_files::Files::new("/", &web_dir)
                    // .show_files_listing()
                    .use_last_modified(true)
                    .index_file("index.html")
            )
    });
    server.bind(format!("0.0.0.0:{}", BG_PORT)).unwrap().run().await?;
    Ok(())
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {

    tokio::spawn(async move {
        tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
        webbrowser::open(&format!("http://localhost:{}/", BG_PORT)).unwrap();
    });

    start_bg().await
}
